/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package opennlp.wrappers;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import opennlp.tools.sentdetect.SentenceDetectorME;
import opennlp.tools.sentdetect.SentenceModel;

/**
 *
 * @author Luis A Riveros
 */
public class SentenceDetectorW {

    private SentenceDetectorME sentenceDetector;
    private OpenNLPModelsManager.LANGUAGE language;

    /**
     * 
     * @param language
     */
    public SentenceDetectorW(OpenNLPModelsManager.LANGUAGE language) {
        this.language = language;
        OpenNLPModelsManager modelsManager = OpenNLPModelsManager.getInstance();
        try {
            InputStream inputStream = modelsManager.loadModel(OpenNLPModelsManager.MODEL_TYPE.SENTENCE_DETECTOR, language);
            SentenceModel sentenceModel = new SentenceModel(inputStream);
            sentenceDetector = new SentenceDetectorME(sentenceModel);
        } catch (FileNotFoundException fnfe) {
            Logger.getLogger(SentenceDetectorW.class.getName()).log(Level.SEVERE, "Model file not found", fnfe);
        } catch (IOException ioe) {
            Logger.getLogger(SentenceDetectorW.class.getName()).log(Level.SEVERE, "Error loading model", ioe);
        }
    }

    private String[] getSentences(String text) {
        return sentenceDetector.sentDetect(text);
    }

    public String getLanguage() {
        return language.getName();
    }

    public static void main(String[] args) {
        String s = "The result array now contains two entires. The first String is First sentence and the second String is Second sentence. The whitespace before, between and after the input String is removed. The API also offers a method which simply returns the span of the sentence in the input string. ";
        SentenceDetectorW sdw = new SentenceDetectorW(OpenNLPModelsManager.LANGUAGE.EN);
        String[] sentences = sdw.getSentences(s);
        for (String sentence : sentences) {
            System.out.println(sentence);
        }

    }
}
